package components

import (
	"eino-script/engine/components/loaders"
	"eino-script/engine/entity"
	"eino-script/engine/types"
	"github.com/cloudwego/eino/components/document"
	"github.com/cloudwego/eino/compose"
)

type LoaderNode struct {
	types.Node
	document.Loader
}

func (l LoaderNode) Id() string {
	return l.Node.Id
}

func (l LoaderNode) Type() (entity.NodeType, error) {
	return l.Node.Type, nil
}

func (l *LoaderNode) GetTargetId() (string, error) {
	return l.Node.Id, nil
}

func (l *LoaderNode) GetSourceId() (string, error) {
	return l.Node.Id, nil
}

func CreateLoaderNode(cfg *entity.Node, g *compose.Graph[any, any]) (types.NodeInterface, error) {
	n, err := CreateGeneralNode(cfg)
	if err != nil {
		return nil, err
	}

	node := &LoaderNode{
		Node: *n,
	}

	id := node.Id()
	if id == "" {
		return nil, err
	}

	loader, err := loaders.CreateLoaderNode(cfg)
	if err != nil {
		return nil, err
	}

	node.Loader = loader

	err = g.AddLoaderNode(id, loader)
	if err != nil {
		return nil, err
	}

	return node, nil
}
